package com.cashmama.market.api.application.controller.actions;

import com.cashmama.market.api.domain.enums.UserProfileActionStatusEnum;
import com.cashmama.market.api.domain.service.ApiUserService;
import com.cashmama.market.api.domain.service.actions.ActionDetailVo;
import com.cashmama.market.api.domain.service.actions.ActionListService;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.infras.enums.JumpRouterEnum;
import com.cashmama.market.api.infras.handler.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2019-12-24
 * Time: 14:26
 */
@RestController
@RequestMapping("/action")
public class ActionListController {

    @Autowired
    private ActionListService actionListService;
    @Autowired
    private ApiUserService apiUserService;

    @RequestMapping("/list")
    public ApiResponse<ActionsResponse> list(@RequestBody @Valid ActionRequest actionRequest) {
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        List<ActionDetailVo> actionList = actionListService.getList(userCode);
        boolean allActionComplete = true;
        for (ActionDetailVo actionDetailVo : actionList) {
            if (actionDetailVo.getStatus() == UserProfileActionStatusEnum.NOT_AUTHORIZED.getCode()) {
                allActionComplete = false;
            }
        }
        String jumpUrl = "";
        if (allActionComplete) {
            jumpUrl = apiUserService.getJumpUrl(true, userCode, actionRequest.getProductCategory(), actionRequest.getMerchantCode());
        }
        ActionsResponse actionsResponse = new ActionsResponse()
                .setActions(actionList)
                .setAllActionComplete(allActionComplete)
                .setJumpUrl(jumpUrl)
                .setButtonJumpEnabled(allActionComplete);
        return new ApiResponse<>(actionsResponse);
    }

    @RequestMapping(value = "/kyc/list")
    public ApiResponse<KycPreResponse> getAadhaarPreInfo() {
        KycPreResponse kycPreResponse = new KycPreResponse();
        kycPreResponse.setTip("Select Your Verification Document");

        List<CardTypePreItem> cardTypePreItems = new ArrayList<>();
        CardTypePreItem aadhaar = new CardTypePreItem("Aadhaar", JumpRouterEnum.KYC_DOCUMENTS_AADHAAR.getJumpUrl());
        cardTypePreItems.add(aadhaar);
        kycPreResponse.setKycList(cardTypePreItems);

        return new ApiResponse<>(kycPreResponse);
    }
}
